<template>
  <a-modal
    v-model="visible"
    :title="WXAuthorized ? $t('all.autotext155.1') : $t('all.autotext155.2')"
    @ok="authorizedSure"
    @cancel="authorizedCancel"
  >
    <template v-if="WXAuthorized">
      <div>{{ $t('all.autotext530') }}</div>
    </template>
    <template v-else>
      <div>{{ $t('all.autotext882') }}</div>
    </template>
    <a-form-model ref="form" :model="form" :rules="rules">
      <a-form-model-item prop="authorizedType" v-if="!WXAuthorized && typeSelect">
        <a-select v-model="form.authorizedType" style="width: 100%;margin-top: 20px;">
          <a-select-option value="1">{{ $t('all.autotext446') }}</a-select-option>
          <a-select-option value="2">{{ $t('subject.autotext2') }}</a-select-option>
        </a-select>
      </a-form-model-item>
    </a-form-model>
  </a-modal>
</template>

<script>
import { apiAuthorizeBank2WX } from '@api/account/user'

export default {
  props: ['pts', 'WXAuthorized'],
  data () {
    return {
      visible: false,
      typeSelect: false,
      rules: {
        authorizedType: [{ required: true, message: this.$t('subject.autotext44'), trigger: 'change' }]
      },
      form: {
        authorizedType: undefined
      }
    }
  },
  methods: {
    open (e, authorizedType) {
      this.typeSelect = !!e
      if (authorizedType) this.form.authorizedType = authorizedType
      this.visible = true
    },
    authorizedSure () {
      this.$refs.form.validate(async valid => {
        if (!valid) return
        await apiAuthorizeBank2WX({
          ptsId: this.pts.id,
          status: this.WXAuthorized ? 0 : 1, // 1-授权 0-取消授权
          authorizedType: this.form.authorizedType
        })
        this.$message.success(this.WXAuthorized ? this.$t('all.autotext501.1') : this.$t('all.autotext501.2'))
        this.$emit('authorizeSuccess')
      })
    },
    authorizedCancel () {
      this.visible = false
      this.typeSelect = false
      this.$refs.form.resetFields()
    }
  }
}
</script>
